package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 运营基础服务：熔断降级处理类
 *
 * @author zgp
 * @version V1.0
 * @date 2025-04-01 17:30
 */
@Component
@Slf4j
public class FoundationsClient {

    @Resource
    private ServeApi serveApi;

    /**
     * 查询服务详情
     * @param id 服务id
     * @return
     */
    @SentinelResource(value = "searchSaveDetail", fallback = "searchSaveDetailFallback", blockHandler = "searchSaveDetailBlock")
    public ServeAggregationResDTO findById(Long id) {
        return serveApi.findById(id);
    }

    /**
     * 降级处理
     *
     * @param e 异常
     * @return
     */
    public ServeAggregationResDTO searchSaveDetailFallback(Throwable e) {
        log.error("远程调用查询服务详情异常降级，异常信息：{}", e.getMessage());
        ServeAggregationResDTO dto = new ServeAggregationResDTO();
        dto.setId(-1L);
        return dto;
    }

    /**
     * 熔断处理
     * @param e 异常
     * @return
     */
    public ServeAggregationResDTO searchSaveDetailBlock(BlockException e) {
        log.error("远程调用查询服务详情异常熔断，异常信息：{}", e.getMessage());
        ServeAggregationResDTO dto = new ServeAggregationResDTO();
        dto.setId(-2L);
        return dto;
    }
}
